﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimeSheetManagement.Models;

namespace TimeSheetManagement.Controllers
{ 
    public class TimeSheetController : Controller
    {
        private tsmdatabaseEntities db = new tsmdatabaseEntities();

        //
        // GET: /TimeSheet/

        public ViewResult Index()
        {
            var timesheets = db.TimeSheets.Include(t => t.Project).Include(t => t.Task).Include(t => t.TimeSheetWeek);
            return View(timesheets.ToList());
        }

        //
        // GET: /TimeSheet/Details/5

        public ViewResult Details(Guid id)
        {
            TimeSheet timesheet = db.TimeSheets.Find(id);
            return View(timesheet);
        }

        //
        // GET: /TimeSheet/Create

        public ActionResult Create()
        {
            ViewBag.ProjectId = new SelectList(db.Projects, "ProjectId", "ProjectName");
            ViewBag.TaskId = new SelectList(db.Tasks, "TaskId", "TaskName");
            ViewBag.TimeSheetWeekId = new SelectList(db.TimeSheetWeeks, "TimeSheetWeekId", "TimeSheetWeekId");
            return View();
        } 

        //
        // POST: /TimeSheet/Create

        [HttpPost]
        public ActionResult Create(TimeSheet timesheet)
        {
            if (ModelState.IsValid)
            {
                timesheet.TimeSheetId = Guid.NewGuid();
                db.TimeSheets.Add(timesheet);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.ProjectId = new SelectList(db.Projects, "ProjectId", "ProjectName", timesheet.ProjectId);
            ViewBag.TaskId = new SelectList(db.Tasks, "TaskId", "TaskName", timesheet.TaskId);
            ViewBag.TimeSheetWeekId = new SelectList(db.TimeSheetWeeks, "TimeSheetWeekId", "TimeSheetWeekId", timesheet.TimeSheetWeekId);
            return View(timesheet);
        }
        
        //
        // GET: /TimeSheet/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            TimeSheet timesheet = db.TimeSheets.Find(id);
            ViewBag.ProjectId = new SelectList(db.Projects, "ProjectId", "ProjectName", timesheet.ProjectId);
            ViewBag.TaskId = new SelectList(db.Tasks, "TaskId", "TaskName", timesheet.TaskId);
            ViewBag.TimeSheetWeekId = new SelectList(db.TimeSheetWeeks, "TimeSheetWeekId", "TimeSheetWeekId", timesheet.TimeSheetWeekId);
            return View(timesheet);
        }

        //
        // POST: /TimeSheet/Edit/5

        [HttpPost]
        public ActionResult Edit(TimeSheet timesheet)
        {
            if (ModelState.IsValid)
            {
                db.Entry(timesheet).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ProjectId = new SelectList(db.Projects, "ProjectId", "ProjectName", timesheet.ProjectId);
            ViewBag.TaskId = new SelectList(db.Tasks, "TaskId", "TaskName", timesheet.TaskId);
            ViewBag.TimeSheetWeekId = new SelectList(db.TimeSheetWeeks, "TimeSheetWeekId", "TimeSheetWeekId", timesheet.TimeSheetWeekId);
            return View(timesheet);
        }

        //
        // GET: /TimeSheet/Delete/5
 
        public ActionResult Delete(Guid id)
        {
            TimeSheet timesheet = db.TimeSheets.Find(id);
            return View(timesheet);
        }

        //
        // POST: /TimeSheet/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {            
            TimeSheet timesheet = db.TimeSheets.Find(id);
            db.TimeSheets.Remove(timesheet);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}